<?php
namespace common\models;

use Yii;
use common\component\tool\Tool;

class App extends \yii\db\ActiveRecord
{
    public static function getDb()
    {
        return Yii::$app->get("common_db");
    }
    
    public static function tableName()
    {
        return '{{%app}}';
    }

    public function rules()
    {
        return [
            ['app_name', 'required', 'message' => 'app_name不能为空', 'on' => 'add'],
            ['app_name', 'string', 'message' => 'app_name必须是字符串',
                         'min' => 2, 'tooShort' => 'app_name最短2位',
                         'max' => 16, 'tooLong' => 'app_name最长16位'],
            ['describe', 'default', 'value' => ''],
            ['describe', 'string', 'message' => 'describe必须是字符串'],
            ['appid', 'default', 'value' => ''],
            ['appid', 'string', 'message' => 'appid必须是字符串',
                      'min' => 18, 'tooShort' => 'appid最短18位',
                      'max' => 18, 'tooLong' => 'appid最长18位'],
            ['appsecret', 'default', 'value' => ''],
            ['appsecret', 'string', 'message' => 'appsecret必须是字符串',
                          'min' => 32, 'tooShort' => 'appsecret最短32位',
                          'max' => 32, 'tooLong' => 'appsecret最长32位'],
            ['comid', 'required', 'message' => 'comid不能为空'],
            ['comid', 'string', 'message' => 'comid必须是字符串',
                      'min' => 8, 'tooShort' => 'comid最短8位',
                      'max' => 8, 'tooLong' => 'comid最长8位'],
            ['state', 'default', 'value' => 0],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(0, 1, 2), 'message' => 'state值必须在0,1,2中选择'],
            ['app_info_id', 'required', 'message' => 'app_info_id不能为空'],
            ['app_info_id', 'integer', 'message' => 'app_info_id必须为数字，或数字字符串', 
                            'min'=>1, 'tooSmall' => 'app_info_id最小存储1', 
                            'max'=>65535, 'tooBig' => 'app_info_id最大存储65535'],
            ['close_reason', 'default', 'value' => ''],
            ['close_reason', 'string', 'message' => 'close_reason必须是字符串'],
            ['expire_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['expire_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'expire_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*
    添加
    $flag 如果是內容管理系統則是“CMS”,如果是及時通訊，則是"IM"
    */
    public function addApp($data, $flag)
    {
        // P($data);
        /*確保APPID总长度为18位*/
        $tmpLength = 4;//如cms需要补充4位
        if(strlen($flag) == 2){
            $tmpLength = 5;
        }
        if(strlen($flag) == 4){
            $tmpLength = 3;
        }
        if(strlen($flag) == 5){
            $tmpLength = 2;
        }
        $data['App']['appid'] = $flag . date('Y', time()) . Tool::generateRandomStr(3) . date('m', time()) . Tool::generateRandomStr($tmpLength) . date('d', time());
        $data['App']['appsecret'] = md5($flag . $data['App']['appid']);
        $data['App']['app_info_id'] = $data['App']['app_info_id'];
        $data['App']['expire_time'] = date('Y-m-d 00:00:00',strtotime('+' . $data['App']['buyTime'] . 'year +1day'));
        $data['App']['add_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                return ['appid' => $data['App']['appid'], 'appsecret' => $data['App']['appsecret']];
            }
        }
        return false;
    }

    
}